package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbGoodsVo;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("goods")
public class GoodsController {
    private Logger logger = LoggerFactory.getLogger(GoodsController.class);
    @Reference
    private GoodsService goodsService;

    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(@RequestParam(defaultValue = "1")Integer number, @RequestParam(defaultValue = "6")Integer pageSize,String sname){
        try {
            PageBean<TbGoodsVo> b = goodsService.getinfo(number,pageSize,sname);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),b);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public dataResult deleteAll(@RequestParam("ids[]")Long[] ids){
        try {
            Integer i = goodsService.deleteAll(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("getSuccess")
    @ResponseBody
    public dataResult getSuccess(@RequestParam("ids[]")Long[] ids){
        try {
            Integer i = goodsService.getSuccess(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("getBack")
    @ResponseBody
    public dataResult getBack(@RequestParam("ids[]")Long[] ids){
        try {
            Integer i = goodsService.getBack(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("content")
    public String content(){
        return "admin/content";
    }

    @RequestMapping("category")
    public String category(){
        return "admin/content_category";
    }
}
